﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AlexandriaPressAuthorBusinessLayer;
using AlexandriaPressAuthorObject;


namespace AlexandriaPressWebsite.MasterPages.AuthorPages
{
    public partial class BookDetails : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            BusinessAuthor bus = new BusinessAuthor();
            if (!Page.IsPostBack)
            {
                if (HttpContext.Current.User.Identity.IsAuthenticated)
                {
                    if (bus.validateUserIfAuthor(bus.getAuthorID(HttpContext.Current.User.Identity.Name)))
                    {
                        BusinessAuthor business = new BusinessAuthor();
                        Book book = new Book();
                        book = business.viewMyBook(Convert.ToInt32(Request.QueryString["id"]), business.getAuthorID(HttpContext.Current.User.Identity.Name));

                        lblBookDatePriceSet.Text = book.DatePriceWasSet.ToString();
                        lblBookDatePublished.Text = book.DatePublished.ToString();
                        lblBookPrice.Text = book.Price.ToString();
                        lblBookStock.Text = book.Stock.ToString();
                        lblBookDescription.Text = book.Description;
                        lblBookGenre.Text = book.Genre;
                        lblBookTitle.Text = book.Title;
                        Session["Book"] = book;

                        butChangePrice.Visible = true;
                        butEditPrice.Visible = false;
                        txtEditPrice.Visible = false;
                        RequiredFieldValidator1.Visible = false;
                    }
                    else
                    {
                        Response.Redirect("~/MasterPages/UserPages/LoggedInUserPage.aspx");
                    }

                }
                else
                {
                    Response.Redirect("~/MasterPages/MainPage.aspx");
                }
            }
        }

        protected void butChangePrice_Click(object sender, EventArgs e)
        {
            Session["TemporaryPrice"] = lblBookPrice.Text;

            lblBookPrice.Visible = false;
            butEditPrice.Visible = true;
            txtEditPrice.Visible = true;
            RequiredFieldValidator1.Visible = true;

            txtEditPrice.Text = Session["TemporaryPrice"].ToString();
            butChangePrice.Visible = false;
        }

        protected void butEditPrice_Click(object sender, EventArgs e)
        {
            butChangePrice.Visible = true;

            BusinessAuthor bus = new BusinessAuthor();
            Book book = (Book)Session["Book"];

            lblBookPrice.Visible = true;
            lblBookPrice.Text = bus.editPrice(book.BookID, Convert.ToInt32(txtEditPrice.Text)).ToString();
            txtEditPrice.Visible = false;
            butEditPrice.Visible = false;
            RequiredFieldValidator1.Visible = false;
  
        }
    }
}
